package cn.ywang.spring.customized.framework.beans;

import cn.ywang.spring.customized.framework.aop.AopConfig;
import cn.ywang.spring.customized.framework.aop.AopProxy;
import cn.ywang.spring.customized.framework.core.FactoryBean;

/**
 * Bean的包装类
 *
 * @author BeatificWang
 */
public class BeanWrapper extends FactoryBean {
	/**
	 * Aop代理
	 */
	private AopProxy aopProxy = new AopProxy();

	/**
	 * 1.支持事件响应，会有一个监听
	 */
	private BeanPostProcessor beanPostProcessor;

	/**
	 * 使用AopProxy动态代理包装后的实例
	 */
	private Object wrapperInstance;

	/**
	 * 原始的通过反射new出来，要进行包装并存储下来
	 */
	private Object originalInstance;

	public BeanWrapper(Object instance) {
		// 把动态代理对象保存进来
		this.wrapperInstance = this.aopProxy.getProxy(instance);
		// 保存原始对象
		this.originalInstance = instance;
	}

	public BeanPostProcessor getBeanPostProcessor() {

		return this.beanPostProcessor;
	}

	public void setBeanPostProcessor(BeanPostProcessor beanPostProcessor) {

		this.beanPostProcessor = beanPostProcessor;
	}

	/**
	 * 返回代理以后的Class
	 * 可能会使这个$Proxy0
	 *
	 * @return
	 */
	public Object getWrapperInstance() {

		return this.wrapperInstance;
	}

	public void setWrapperInstance(Object wrapperInstance) {
		this.wrapperInstance = wrapperInstance;
	}

	public Object getOriginalInstance() {
		return this.originalInstance;
	}

	public void setOriginalInstance(Object originalInstance) {
		this.originalInstance = originalInstance;
	}

	public void setAopConfig(AopConfig aopConfig) {
		this.aopProxy.setAopConfig(aopConfig);
	}

	public Class<?> getWrapperClass() {
		return this.wrapperInstance.getClass();
	}
}
